package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.SServiceOrderStatusEnum;
import cn.huiyunche.base.service.enums.SServiceOrderTrailTypeEnum;
import cn.huiyunche.base.service.interfaces.SServiceOrderTrailService;
import cn.huiyunche.base.service.interfaces.SServiceOrderVehicleService;
import cn.huiyunche.base.service.mappers.SServiceOrderVehicleMapper;
import cn.huiyunche.base.service.mappers.ext.SServiceOrderVehicleExtMapper;
import cn.huiyunche.base.service.model.SServiceOrderTrail;
import cn.huiyunche.base.service.model.SServiceOrderVehicle;
import cn.huiyunche.base.service.model.SServiceOrderVehicleExample;
import cn.huiyunche.base.service.vo.SOrderDetailVo;
import cn.huiyunche.base.service.vo.SServiceOrderVehicleVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SServiceOrderVehicleServiceImpl
 * @Description: 服务订单车辆信息记录
 * @author: Aaron
 * @date: 2016年10月8日 下午5:06:03
 */
@Service
public class SServiceOrderVehicleServiceImpl implements SServiceOrderVehicleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SServiceOrderVehicleServiceImpl.class);

    @Autowired
    private SServiceOrderVehicleMapper sServiceOrderVehicleMapper = null;

    @Autowired
    private SServiceOrderVehicleExtMapper sServiceOrderVehicleExtMapper;

    @Autowired
    private SServiceOrderTrailService sServiceOrderTrailService;

    public SServiceOrderVehicleMapper getSServiceOrderVehicleMapper() {
        return this.sServiceOrderVehicleMapper;
    }

    @Override
    public long add(SServiceOrderVehicle orderVehicle) {
        LOGGER.info("prarm : {}", orderVehicle);
        this.getSServiceOrderVehicleMapper().insertSelective(orderVehicle);
        return orderVehicle.getId();
    }

    /**
     * 根据服务订单ID 查询vehicleID
     *
     * @param sorderId
     * @return
     */
    @Override
    public SServiceOrderVehicle selectByServiceOrderId(String serviceOrderId) {
        try {
            SServiceOrderVehicleExample example = new SServiceOrderVehicleExample();
            example.createCriteria().andServiceOrderIdEqualTo(Long.valueOf(serviceOrderId));
            List<SServiceOrderVehicle> list = this.getSServiceOrderVehicleMapper().selectByExample(example);
            if (list.size() > 0) {
                return list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<SOrderDetailVo> getListByCOrderId(long customerOrderId, Integer dispatchTimes, List<Integer> status) {
        LOGGER.info("getListByCOrderId param : {}", customerOrderId);
        if (0 == customerOrderId) {
            LOGGER.error("getListByCOrderId customerOrderId must not be 0");
            throw new IllegalArgumentException("客户订单id不能是0");
        }
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("customerOrderId", customerOrderId);
        if (null != dispatchTimes) {
            paramsMap.put("dispatchTimes", dispatchTimes);
        }
        if (CollectionUtils.isNotEmpty(status)) {
            paramsMap.put("status", status);
        }
        List<SOrderDetailVo> list = sServiceOrderVehicleExtMapper.getListByConditions(paramsMap);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(vo -> {
                SServiceOrderTrail trail = sServiceOrderTrailService.getNewest(vo.getOrderId(), SServiceOrderTrailTypeEnum.SERVICE_ORDER_ADDR.getValue());
                if (null != trail) {
                    if (StringUtils.isNotBlank(trail.getInfo())) {
                        vo.setPostion(trail.getInfo());
                    }
                }
                // vo.setStatusText(SServiceOrderStatusEnum.getByValue(vo.getStatus()).getText());
                vo.setStatusText(SServiceOrderStatusEnum.getByValue(vo.getStatus()).getText());
            });

            return list;
        }
        return null;
    }

    @Override
    public List<SServiceOrderVehicleVo> getListBySOrderIds(List<Long> serviceOrderIds) {
        LOGGER.info("getListBySOrderIds params : {}", serviceOrderIds);
        if (CollectionUtils.isEmpty(serviceOrderIds)) {
            LOGGER.error("getListBySOrderIds serviceOrderIds must not be null");
            throw new IllegalArgumentException("服务订单主键不能为空");
        }

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("serviceOrderIds", serviceOrderIds);

        List<SServiceOrderVehicleVo> list = sServiceOrderVehicleExtMapper.selectByConditions(paramMap);
        if (CollectionUtils.isEmpty(list)) {
            LOGGER.error("getListBySOrderIds list must not be null");
            throw new BusinessException("获取服务订单车辆信息失败");
        }
        return list;
    }

    @Override
    public int delete(Long serviceOrderId) {
        LOGGER.info("delete param {}", serviceOrderId);
        if (null == serviceOrderId) {
            LOGGER.error("delete serviceOrderId must not be null");
            throw new IllegalArgumentException("服务订单id不能为空");
        }

        SServiceOrderVehicleExample example = new SServiceOrderVehicleExample();
        example.createCriteria().andServiceOrderIdEqualTo(serviceOrderId);
        int count = sServiceOrderVehicleMapper.deleteByExample(example);

        return count;
    }
}
